# coding: utf8

import unittest


def list_counter(a, b):
    """
    my function used to illustrate test

    Args:
        a(list[int]): sequence to count the number of some number b
        b(int) : some element used to count in a
    Return:
        int: sum of a
"""

    if b not in a:
        return 0
    else:
        return sum([1 for x in a if x == b])


class TestCounter(unittest.TestCase):

    def setUp(self):
        print("test start")
        self.test_list = [1, 2, 1]
        self.test_element1 = 1
        self.test_element2 = 2
        self.test_element3 = 0

    def test_count_many(self):
        result = list_counter(self.test_list, self.test_element1)
        self.assertEqual(result, 2)
        print("test_count_many ok!")

    def test_count_one(self):
        result = list_counter(self.test_list, self.test_element2)
        self.assertEqual(result, 1)
        print("test_count_one ok!")

    def test_count_no(self):
        result = list_counter(self.test_list, self.test_element3)
        self.assertEqual(result, 0)
        print("test_count_no ok!")

    def tearDown(self):
        print("test end")


if __name__ == '__main__':
    # 构建测试集
    # test_cases = unittest.TestLoader().loadTestsFromTestCase(TestCounter)
    # suite = unittest.TestSuite()
    # suite.addTests(test_cases)

    suite = unittest.TestSuite()
    suite.addTest(TestCounter("test_count_many"))
    suite.addTest(TestCounter("test_count_one"))
    suite.addTest(TestCounter("test_count_no"))

    # 执行测试
    runner = unittest.TextTestRunner(verbosity=2)
    result = runner.run(suite)

    # 输出结果
    print("test Report:")
    print("test runs:", result.testsRun)
    print("test failures:", result.failures)
    print("test errors:", result.errors)
    print("test skipped:", result.skipped)
